package leet;

/**
 * Long Pressed Name
 * 
 * <pre>
 * Your friend is typing his name into a keyboard. Sometimes, when typing a
 * character c, the key might get long pressed, and the character will be typed
 * 1 or more times.
 * 
 * You examine the typed characters of the keyboard. Return True if it is
 * possible that it was your friends name, with some characters (possibly none)
 * being long pressed.
 * </pre>
 * 
 * @author zhujunbing
 * @date 2019年4月24日
 */
public class Leet0925 {

	public static void main(String[] args) {
		Leet0925 leet925 = new Leet0925();
		String name = "alex";
		String typed = "aaleex";

		boolean longPressedName = leet925.isLongPressedName(name, typed);
		System.out.println(longPressedName);
	}

	/**
	 * 0ms
	 * 
	 * @param name
	 * @param typed
	 * @return
	 * @date 2019年4月24日
	 */
	public boolean isLongPressedName(String name, String typed) {

		if (name.equals(typed)) {
			return true;
		}
		int namel = name.length();
		int typedl = typed.length();

		if (namel > typedl) {
			return false;
		}

		char[] names = name.toCharArray();

		char[] typeds = typed.toCharArray();

		int j = 0;
		char pre = names[0];

		//
		for (int i = 0; i < namel; i++) {
			while (typeds[j] != names[i]) {
				if (typeds[j] != pre) {
					return false;
				}
				j++;
				if (namel - i > typedl - j) {
					return false;
				}
			}
			j++;
			pre = names[i];
		}

		return true;
	}
}
